<?php

namespace App\Http\Controllers;

use App\Models\Announcement;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests\Logic\Announcement\AnnouncementCreate;
use App\Http\Requests\Logic\Announcement\AnnouncementDestroy;
use App\Http\Requests\Logic\Announcement\AnnouncementUpdate;
use App\Http\Requests\Logic\Announcement\QueryResource;
use App\Http\Requests\Logic\Announcement\ShowRequest;
use App\Repositories\AnnouncementRepository;


class AnnouncementController extends Controller
{
    protected $announcementRepository;

    /**
     * AnnouncementController constructor.
     * @param AnnouncementRepository $announcementRepository
     */
    public function __construct(
        AnnouncementRepository $announcementRepository
    )
    {
        $this->announcementRepository = $announcementRepository;
    }

    /**
     * 获取通知资源列表
     *
     * Date: 2018/4/20
     * @author Tenny
     * @param QueryResource $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(QueryResource $request)
    {
        $limit = $request->get('paginate', 15);
        $title = $request->get('title', false);
        $time = $request->get('time', false);
        $author = $request->get('author', false);
        $totle = $request->get('totle', 1);
        $user = Auth::user();
        $uid = $user->id;
        if ($totle == 0){
            $announcements = $this->announcementRepository->getUserAnnouncements($uid, $title, $time, $author, $limit);
        }else{
            $announcements = $this->announcementRepository->getTotalAnnouncements($title, $time, $author, $limit);
        }
        return success($announcements);
    }

    /**
     * 根据通知Id 获取通知详情资源
     *
     * Date: 2018/4/20
     * @author Tenny
     * @param ShowRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(ShowRequest $request, $announcement)
    {
        $user = Auth::user();
        $company_id = $user->company_id;
        $uid = $user->id;
        $res = $this->announcementRepository->getAnnouncementDetails($announcement, $uid);
        return success($res);
    }


    /**
     * 新建通知资源
     *
     * Date: 2018/4/20
     * @author Tenny
     * @param AnnouncementCreate $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function store(AnnouncementCreate $request)
    {
        $this->extractInputFromRules($request, $request->rules());
        $user_id = $request->user()->id;
        $project = $request->get('project', "[]");
        $project_arr = json_decode($project);
        $title = $request->get('title', false);
        $content = $request->get('content', false);
        if( ! is_array($project_arr) || count($project_arr) < 1){
            return failed('未选择通知的范围',4001);
        }
        $res = $this->announcementRepository->createAnnouncement($user_id, $title, $content, $project_arr);
        if ($res === true){
            return success([]);
        }else{
            return failed('请稍后再试',500);
        }
    }

    /**
     * 更新通知资源
     *
     * Date: 2018/4/20
     * @author Tenny
     * @param AnnouncementUpdate $request
     * @param Announcement $announcement
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(AnnouncementUpdate $request, $announcement)
    {
        $this->extractInputFromRules($request, $request->rules());
        $project = $request->get('project', "[]");
        $project_arr = json_decode($project);
        $title = $request->get('title', false);
        $content = $request->get('content', false);
        $res = $this->announcementRepository->updateAnnouncement($announcement, $title, $content, $project_arr);
        if ($res === true){
            return success([]);
        }else{
            return failed('请稍后再试',500);
        }
    }

    /**
     * 删除 通知 资源
     *
     * Date: 2018/4/20
     * @author Tenny
     * @param AnnouncementDestroy $request
     * @param Announcement $article
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy(AnnouncementDestroy $request, Announcement $announcement)
    {
        $force = $request->get('force');
        \DB::beginTransaction();
        try {
            $force ? $announcement->delete() : $announcement->forceDelete();
            //删除通知范围  关联表announcement_project_pivot
            \DB::table('announcement_project_pivot')
                ->where('announcement_id', $announcement->id)
                ->delete();
            //删除已读关联表  announcement_user_pivot
            \DB::table('announcement_user_pivot')
                ->where('announcement_id', $announcement->id)
                ->delete();
            \DB::commit();
        } catch(\Illuminate\Database\QueryException $ex) {
            \DB::rollback();
            return failed('未知错误',500);
        }
        return deleted();
    }

}
